<template>
  <view class="container">
    <!-- 左上角返回按钮 -->
    <view class="menu-btn" style="z-index: 102">
      <view class="dot-text Flex iconfont icon-fanhui" @click="goBack"></view>
      <picker mode="selector" :range="colors" @change="onColorChange">
        <view class="dot-text Flex iconfont icon-palette-line"></view>
      </picker>
      <view
        class="dot-text Flex iconfont icon-shezhi"
        @click.stop="openInput"
      ></view>
    </view>

    <!-- 右上角设置按钮 -->
    <view class="topBtn"> </view>

    <canvas type="2d" id="myCanvas" style="z-index: 0" class="canvas"></canvas>

    <!-- 弹窗蒙层 -->
    <view class="modal-overlay" v-show="showInput" @click="cancelInput"></view>

    <!-- 输入框弹窗 -->
    <view class="input-box" v-show="showInput">
      <view class="title">✨ 自定义字符 ✨</view>
      <input type="text" v-model="tempText" placeholder="请输入要显示的字符" />
      <view class="button-group">
        <button class="cancel-btn" @click="cancelInput">取消</button>
        <button class="confirm-btn" @click="confirmInput">确定</button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      ctx: null,
      width: 0,
      height: 0,
      arr: [],
      str: "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split(""),
      offTimer: null,
      currentColor: "#39b54a",
      colors: [
        "#8799a3",
        "#6739b6",
        "#e03997",
        "#e54d42",
        "#fbbd08",
        "#f37b1d",
        "#39b54a",
        "#1cbbb4",
        "#a5673f",
        "#8dc63f",
        "#0081ff",
        "#fbc21a",
        "#b73feb"
      ],
      showInput: false,
      tempText: "",
    };
  },

  onReady() {
    this.initCanvas();
  },

  methods: {
    async initCanvas() {
      // 获取系统信息
      const sysInfo = uni.getSystemInfoSync();
      this.width = sysInfo.windowWidth;
      this.height = sysInfo.windowHeight;

      // 获取canvas上下文
      const query = uni.createSelectorQuery().in(this);
      query
        .select("#myCanvas")
        .fields({ node: true, size: true })
        .exec((res) => {
          const canvas = res[0].node;
          const ctx = canvas.getContext("2d");

          // 设置canvas尺寸
          canvas.width = this.width;
          canvas.height = this.height;

          this.ctx = ctx;
          this.arr = Array(Math.ceil(this.width / 10)).fill(0);

          // 初始化动画
          this.init();
        });
    },

    rain() {
      const ctx = this.ctx;
      ctx.fillStyle = "rgba(0,0,0,0.05)";
      ctx.fillRect(0, 0, this.width, this.height);
      ctx.fillStyle = this.currentColor;

      this.arr.forEach((value, index) => {
        ctx.fillText(
          this.str[Math.floor(Math.random() * this.str.length)],
          index * 10,
          value + 10
        );
        this.arr[index] =
          value >= this.height || value > 8888 * Math.random() ? 0 : value + 10;
      });
    },

    init() {
      // 清除之前的定时器
      if (this.offTimer) {
        clearInterval(this.offTimer);
      }

      // 清空画布
      this.ctx.clearRect(0, 0, this.width, this.height);

      // 启动新的动画
      this.offTimer = setInterval(() => {
        this.rain();
      }, 50);
    },

    onColorChange(e) {
      this.currentColor = this.colors[e.detail.value];
      this.init();
    },

    goBack() {
      uni.navigateBack();
    },

    openInput() {
      this.showInput = true;
      this.tempText = this.str.join("");
    },

    cancelInput() {
      this.showInput = false;
      this.tempText = "";
    },

    confirmInput() {
      if (this.tempText.trim()) {
        this.str = this.tempText.split("");
        // 重新初始化动画
        this.init();
      }
      this.showInput = false;
      this.tempText = "";
    },
  },

  onUnmounted() {
    if (this.offTimer) {
      clearInterval(this.offTimer);
    }
  },
};
</script>

<style>
.container {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.canvas {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1;
}

.topBtn {
  position: fixed;
  top: var(--status-bar-height);
  right: 20rpx;
  z-index: 101;
}

.color-picker {
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.2);
  padding: 10rpx 20rpx;
  border-radius: 30rpx;
}

.color-picker text {
  color: #fff;
  font-size: 24rpx;
  margin-right: 10rpx;
}

.color-preview {
  width: 30rpx;
  height: 30rpx;
  border-radius: 50%;
  border: 2rpx solid rgba(255, 255, 255, 0.5);
}

.menu-btn {
  position: fixed;
  top: 9rpx;
  left: 20rpx;
  min-width: 70px;
  height: 32px;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12rpx;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  z-index: 100;
}
.dot-text {
  font-size: 18rpx;
  width: 40px;
  height: 100%;
}
.dot {
  width: 6rpx;
  height: 6rpx;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.6);
}

.menu-btn:active {
  background: rgba(255, 255, 255, 0.2);
}
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(8px);
  z-index: 998;
}

.input-box {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(15px);
  padding: 20rpx;
  width: 500rpx;
  box-sizing: border-box;
  border-radius: 30rpx;
  text-align: center;
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  z-index: 999;
}

.input-box .title {
  color: #fff;
  font-size: 32rpx;
  margin-bottom: 30rpx;
  font-weight: 500;
}

.input-box input {
  height: 88rpx;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 16rpx;
  padding: 0 30rpx;
  color: #fff;
  font-size: 28rpx;
  margin-bottom: 30rpx;
}

.input-box input::placeholder {
  color: rgba(255, 255, 255, 0.5);
}

.button-group {
  display: flex;
  justify-content: space-between;
  gap: 30rpx;
}

.input-box button {
  width: 45%;
  height: 62rpx;
  line-height: 62rpx;
  padding: 0 40rpx;
  border-radius: 36rpx;
  font-size: 28rpx;
  font-weight: normal;
  transition: all 0.3s ease;
}

.cancel-btn {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.confirm-btn {
  background: linear-gradient(135deg, #007aff, #00c6ff);
  border: none;
}

.input-box button:active {
  transform: scale(0.98);
  opacity: 0.9;
}
</style>